package io.github.generator.strategy;

import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import io.github.common.consts.StringPool;
import io.github.common.util.StringUtils;
import io.github.generator.CodeGenerator;
import io.github.generator.util.GeneratorUtils;

/**
 * 自定义文件策略
 *
 * @author haijun
 * @version 1.0.0
 * @email "mailto:haijun@qq.com"
 * @date 2024.11.09 19:55
 * @since 1.0.0
 */
public class CustomFileInjectionStrategy implements Strategy<CodeGenerator, InjectionConfig.Builder> {

    /**
     * Template switch
     */
    private final CodeGenerator.TemplateSwitch templateSwitch;

    /**
     * Custom file injection strategy
     *
     * @param templateSwitch template switch
     * @since 1.0.0
     */
    public CustomFileInjectionStrategy(CodeGenerator.TemplateSwitch templateSwitch) {
        this.templateSwitch = templateSwitch;
    }

    /**
     * Builder
     *
     * @param generator generator
     * @param builder   builder
     * @since 1.0.0
     */
    @Override
    public void builder(CodeGenerator generator, InjectionConfig.Builder builder) {
        if (!this.templateSwitch.isDisableEnum()) {
            builder.customFile(this.buildCustomFile(generator, builder, "dto", "DTO"));
        }
        if (!this.templateSwitch.isDisableDto()) {
            builder.customFile(this.buildCustomFile(generator, builder, "dto", "DTO"));
        }
        if (!this.templateSwitch.isDisableReq()) {
            builder.customFile(this.buildCustomFile(generator, builder, "req", "Req"));
        }
        if (!this.templateSwitch.isDisableForm()) {
            builder.customFile(this.buildCustomFile(generator, builder, "form", "Form"));
        }
    }

    /**
     * Build custom file
     *
     * @param generator   generator
     * @param builder     builder
     * @param packagePath package path
     * @param fileName    file name
     * @return the custom file
     * @since 1.0.0
     */
    private CustomFile buildCustomFile(CodeGenerator generator, InjectionConfig.Builder builder, String packagePath, String fileName) {
        String templateFilePath = GeneratorUtils.getTemplateFilePath(generator.getTemplatePath(),
                StringUtils.lowerCase(fileName) + StringPool.DOT + "java");
        String path = GeneratorUtils.getPackagePath("entity", generator.getModelPackagePath(), packagePath);
        return new CustomFile.Builder()
                .fileName(fileName)
                .packageName(path)
                .templatePath(templateFilePath + StringPool.DOT + "vm")
                .build();
    }
}
